package com.winshare.saleAssistant.util;

import java.util.List;

public class LogicResponse<T> {

    private T body;
    private List<T> data;
    private Page<T> pageInfo;
    private LogicStatus status;

    public static LogicResponse.Builder status(LogicStatus logicStatus) {
        return new LogicResponse.DefaultBuilder(logicStatus);
    }

    public static LogicResponse.Builder success() {
        return new LogicResponse.DefaultBuilder(LogicStatus.SUCCESS);
    }

    public static <T> LogicResponse<T> success(T body) {
        return success().body(body);
    }

    public static <T> LogicResponse<T> success(List<T> data) {
        return success().body(data);
    }

    public static <T> LogicResponse<T> success(Page<T> pageInfo) {
        return success().body(pageInfo);
    }

    public static <T> LogicResponse<T> success(T body, List<T> data) {
        return success().body(body, data);
    }

    public static <T> LogicResponse<T> success(T body, Page<T> pageInfo) {
        return success().body(body, pageInfo);
    }

    public static LogicResponse.Builder failure() {
        return new LogicResponse.DefaultBuilder(LogicStatus.FAILURE);
    }

    public static LogicResponse.Builder failure(String reasonPhrase) {
        return new LogicResponse.DefaultBuilder(LogicStatus.FAILURE.mutate(reasonPhrase));
    }

    public static LogicResponse.Builder paramError() {
        return new LogicResponse.DefaultBuilder(LogicStatus.PARAM_ERROR);
    }

    public static LogicResponse.Builder paramError(String reasonPhrase) {
        return new LogicResponse.DefaultBuilder(LogicStatus.PARAM_ERROR.mutate(reasonPhrase));
    }

    public static LogicResponse.Builder unauthorized() {
        return new LogicResponse.DefaultBuilder(LogicStatus.UNAUTHORIZED);
    }

    public static LogicResponse.Builder unauthorized(String reasonPhrase) {
        return new LogicResponse.DefaultBuilder(LogicStatus.UNAUTHORIZED.mutate(reasonPhrase));
    }

    public static LogicResponse.Builder notFound() {
        return new LogicResponse.DefaultBuilder(LogicStatus.NOT_FOUND);
    }

    public static LogicResponse.Builder notFound(String reasonPhrase) {
        return new LogicResponse.DefaultBuilder(LogicStatus.NOT_FOUND.mutate(reasonPhrase));
    }

    public static LogicResponse.Builder serverError() {
        return new LogicResponse.DefaultBuilder(LogicStatus.SERVER_ERROR);
    }

    public static LogicResponse.Builder serverError(String reasonPhrase) {
        return new LogicResponse.DefaultBuilder(LogicStatus.SERVER_ERROR.mutate(reasonPhrase));
    }

    private LogicResponse(LogicStatus status) {
        this.status = status;
    }

    private LogicResponse(T body, LogicStatus status) {
        this.body = body;
        this.status = status;
    }

    private LogicResponse(List<T> data, LogicStatus status) {
        this.data = data;
        this.status = status;
    }

    private LogicResponse(Page<T> pageInfo, LogicStatus status) {
        this.pageInfo = pageInfo;
        this.status = status;
    }

    private LogicResponse(T body, List<T> data, LogicStatus status) {
        this.body = body;
        this.data = data;
        this.status = status;
    }

    private LogicResponse(T body, Page<T> pageInfo, LogicStatus status) {
        this.body = body;
        this.pageInfo = pageInfo;
        this.status = status;
    }

    private LogicResponse() {
    }

    public LogicStatus getStatus() {
        return this.status;
    }

    public T getBody() {
        return this.body;
    }

    public List<T> getData() {
        return this.data;
    }

    public Page<T> getPageInfo() {
        return this.pageInfo;
    }

    public int embeddedValue() {
        return this.status.getValue();
    }

    public String embeddedReasonPhrase() {
        return this.status.getReasonPhrase();
    }

    private boolean isOK() {
        return this.status.getValue() == LogicStatus.SUCCESS.getValue();
    }

    private static class DefaultBuilder implements LogicResponse.Builder {
        private LogicStatus status;

        DefaultBuilder(LogicStatus status) {
            this.status = status;
        }

        public <T> LogicResponse<T> build() {
            return new LogicResponse(this.status);
        }

        public <T> LogicResponse<T> body(T body) {
            return new LogicResponse(body, this.status);
        }

        public <T> LogicResponse<T> body(List<T> data) {
            return new LogicResponse(data, this.status);
        }

        public <T> LogicResponse<T> body(Page<T> pageInfo) {
            return new LogicResponse(pageInfo, this.status);
        }

        public <T> LogicResponse<T> body(T body, List<T> data) {
            return new LogicResponse(body, data, this.status);
        }

        public <T> LogicResponse<T> body(T body, Page<T> pageInfo) {
            return new LogicResponse(body, pageInfo, this.status);
        }
    }

    public interface Builder {
        <T> LogicResponse<T> build();

        <T> LogicResponse<T> body(T var1);

        <T> LogicResponse<T> body(List<T> var1);

        <T> LogicResponse<T> body(Page<T> var1);

        <T> LogicResponse<T> body(T var1, List<T> var2);

        <T> LogicResponse<T> body(T var1, Page<T> var2);
    }
}
